﻿using System.Collections.Generic;
using MockDataTransferObject;
using System.Data.SqlClient;
using System;
using MockCommon;
using System.Data;
using System.Linq;
using System.Collections.ObjectModel;

namespace MockDataAccess
{
    public class ContractorDataAccess : BaseDataAccess<Contractor>
    {
        private Collection<SqlParameter> _parameters;
        
        /// <summary>
        /// Initializes a new instance of the <see cref="ContractorDataAccess"/> class.
        /// </summary>
        public ContractorDataAccess()
            : base()
        {
        }
        
        /// <summary>
        /// Initializes a new instance of the <see cref="ContractorDataAccess"/> class.
        /// </summary>
        /// <param name="isFake">if set to <c>true</c> [is fake].</param>
        public ContractorDataAccess(bool isFake)
            : base(isFake)
        {
        }

        #region Is
        #endregion

        #region Get
        /// <summary>
        /// Retrieves all contractors.
        /// </summary>
        /// <returns></returns>
        public Collection<Contractor> RetrieveAllContractors()
        {
            try
            {
                DataTable dataTable = ExecuteQueryAsDataTable(Constant.ContractorGetAll, null);
                Collection<AbstractEntity> entities = ConvertToList(dataTable, typeof(Contractor));
                return new Collection<Contractor>(entities.Cast<Contractor>().ToList());
            }
            catch (ArgumentException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
            catch (InvalidCastException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
        }

        /// <summary>
        /// Gets the name of the by user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <returns></returns>
        public Contractor GetByUserName(string userName)
        {
            try
            {
                _parameters = new Collection<SqlParameter>();
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterUserName, Value = userName });

                DataTable dataTable = ExecuteQueryAsDataTable(Constant.ContractorGetByUserName, _parameters);
                Collection<AbstractEntity> contractors = ConvertToList(dataTable, typeof(Contractor));
                if (contractors != null && contractors.Count > 0)
                {
                    return (Contractor)contractors[0];
                }
            }
            catch (InvalidCastException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
            return null;
        }

        /// <summary>
        /// Gets the by consultant id.
        /// </summary>
        /// <param name="consultantId">The consultant id.</param>
        /// <returns></returns>
        public Collection<Contractor> GetByConsultantId(object consultantId)
        {
            try
            {
                _parameters = new Collection<SqlParameter>();
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterConsultantId, Value = consultantId });
                DataTable table = ExecuteQueryAsDataTable(Constant.ContractorGetByConsultantId, _parameters);
                Collection<AbstractEntity> entities = ConvertToList(table, typeof(Contractor));
                return new Collection<Contractor>(entities.Cast<Contractor>().ToList());
            }
            catch (ArgumentException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
            catch (InvalidCastException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
        }

        /// <summary>
        /// Gets the by filter.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="email">The email.</param>
        /// <param name="firstName">The first name.</param>
        /// <param name="lastName">The last name.</param>
        /// <param name="isActive">The is active.</param>
        /// <param name="consultantId">The consultant id.</param>
        /// <returns></returns>
        public Collection<Contractor> GetByFilter(object userName, object email, object firstName, object lastName, object isActive, object consultantId)
        {
            try
            {
                _parameters = new Collection<SqlParameter>();
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterUserName, Value = userName });
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterEmail, Value = email });
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterFirstName, Value = firstName });
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterLastName, Value = lastName });
                if (isActive != null)
                {
                    bool isActiveBool = (bool)isActive;
                    _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterIsActive, Value = isActiveBool ? "1" : "0" });
                }
                _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterConsultantId, Value = consultantId });
                DataTable dataTable = ExecuteQueryAsDataTable(Constant.ContractorGetByFilter, _parameters);
                Collection<AbstractEntity> entities = ConvertToList(dataTable, typeof(Contractor));
                return new Collection<Contractor>(entities.Cast<Contractor>().ToList());
            }
            catch (ArgumentException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
            catch (InvalidCastException exception)
            {
                throw new MockException(exception.Message, "DataAccess");
            }
        }
        #endregion
    }
}
